package com.android.stm;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;

public class BallView extends View{
	
	int width,height;
	private Paint circlePaint,circlePaint1,circlePaint2,circlePaint3;
	private Drawable[] mDrawable;
	private RefreshHandler mRedrawHandler=new RefreshHandler();;
	private int[] posx,posy; 
	
	public BallView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initCalview();
	}
	public BallView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initCalview();
	}
	public BallView(Context context) {
		super(context);
		initCalview();
	}
	
	protected void initCalview()
	{
		mDrawable=new Drawable[4];
		posx=new int[16];
		posy=new int[16];
		mDrawable[0] = this.getResources().getDrawable(R.drawable.bluen);
		mDrawable[1] = this.getResources().getDrawable(R.drawable.greenn);
		mDrawable[2] = this.getResources().getDrawable(R.drawable.redn);
		mDrawable[3] = this.getResources().getDrawable(R.drawable.yellown);
		
		for(int i=0;i<16;i++)
		{
			posx[i]=(int)(Math.random() * (250- 1 + 1) ) + 1;
			posy[i]=(int)(Math.random() * (250 - 1 + 1) ) + 1;
		}
		
	}
	
	@Override
	protected void onMeasure(int widthSpec, int heightSpec)
	{
		int specWidth=MeasureSpec.getSize(widthSpec);
		int specHeight=MeasureSpec.getSize(heightSpec);
		setMeasuredDimension(specWidth,specHeight);
		set_pos();
	}
	
	public void set_pos()
	{
		int x;
		int px=this.getMeasuredWidth();
		int py=this.getMeasuredHeight();
		width=(int)px;
		height=(int)py;
		
		this.layout(width/2, 0, 0, 0);
		circlePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setColor(Color.BLUE);
		circlePaint.setStrokeWidth(1);
		circlePaint.setStyle(Paint.Style.STROKE);
		
		circlePaint1=new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint1.setStrokeWidth(1);
		circlePaint1.setStyle(Paint.Style.STROKE);
		
		circlePaint2=new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint2.setStrokeWidth(1);
		circlePaint2.setStyle(Paint.Style.STROKE);
		
		circlePaint3=new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint3.setStrokeWidth(1);
		circlePaint3.setStyle(Paint.Style.STROKE);
				
		
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{		
		int rad=Math.min(width/4, height/4);
		canvas.drawCircle(width/4,height/4, rad, circlePaint);
		circlePaint1.setColor(Color.RED);
		canvas.drawCircle(width-rad,height-rad, rad, circlePaint1);
		circlePaint2.setColor(Color.YELLOW);
		canvas.drawCircle(width-rad,height/4,rad/2, circlePaint2);
		circlePaint3.setColor(Color.GREEN);
		canvas.drawCircle(width/4,height-rad, rad/2, circlePaint3);


		for(int i=0;i<8;i++)
		{
			mDrawable[0].setBounds(posx[i],posy[i],posx[i]+35,posy[i]+35);
			mDrawable[0].draw(canvas);
			i++;
			mDrawable[1].setBounds(posx[i],posy[i],posx[i]+35,posy[i]+35);
			mDrawable[1].draw(canvas);
			i++;
			mDrawable[2].setBounds(posx[i],posy[i],posx[i]+35,posy[i]+35);
			mDrawable[2].draw(canvas);
			i++;
			mDrawable[3].setBounds(posx[i],posy[i],posx[i]+35,posy[i]+35);
			mDrawable[3].draw(canvas);
			
			mRedrawHandler.sleep(1000);
			
		}
		
		
	}
	
	class RefreshHandler extends Handler {

	    @Override
	    public void handleMessage(Message msg) {
	       	for(int i=0;i<8;i++)
	    	{
	       		posy[i]=posy[i]+20;
	       		posx[i]=(int)(Math.random() * (250- 1 + 1) ) + 1;
	       		if(posy[i]<height)
	       		{}
	       		else
	       		{posy[i]=0;}
	       		
	        }
	       	BallView.this.invalidate();
	    }

	    public void sleep(long delayMillis) {
	            this.removeMessages(0);
	        sendMessageDelayed(obtainMessage(0), delayMillis);
	    }
	};
}
